#coding: utf8
import re
import locale
from pygeocoder import Geocoder
import time
import urlparse
from datetime import datetime
from scrapy.http import Request
from scrapy.http import FormRequest
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from fresh.items import RestaurantItem
from fresh.items import RestaurantReview

class PaginasUtils(object):

    utils = None

    def fillRestaurant(self,response,hxs,restaurant):
        try:
            

            #from scrapy.shell import inspect_response
            #inspect_response(response)



            restaurant['url_scrapy']=hxs.select('div[@class="m-results-business-section"]/div[@class="m-results-business-header"]/h3/a/@href').extract()
            restaurant['restaurant_name'] = hxs.select('div[@class="m-results-business-section"]/div[@class="m-results-business-header"]/h3/a/span/text()').extract()
            restaurant['city']='NA'                
            
            address0 = hxs.select('div[@class="m-results-business-section"]/p[@itemprop="address"]/span[@itemprop="streetAddress"]/text()').extract()
            address1 = hxs.select('div[@class="m-results-business-section"]/p[@itemprop="address"]/span[@itemprop="postalCode"]/text()').extract()
            address2 = hxs.select('div[@class="m-results-business-section"]/p[@itemprop="address"]/span[@class="locality"]/text()').extract()
            address = self.utils.convertListToString(address0) + ", " + self.utils.convertListToString(address1)+ ", " +self.utils.convertListToString(address2)
            restaurant['address'] = address
            restaurant['address_google'] = self.utils.getRestAddressGoogleReady(address)
            restaurant['phone'] = hxs.select('div[@class="m-results-business-section last"]/ul/li[@class="is-omega"]/a/span[@class="m-icon--single-phone"]/text()').extract()
            restaurant['cp']= hxs.select('div[@class="m-results-business-section"]/p[@itemprop="address"]/span[@itemprop="postalCode"]/text()').extract()
            restaurant['url_rest']=hxs.select('div[@class="m-results-business-section"]/dl/dd/a/text()').extract()
            restaurant['email']=self.__getEmail(self.utils.convertListToString(hxs.select('div[@class="m-results-business-section last"]/ul/li[@class="is-maxi"]/a/@href').extract()))
            restaurant['score']=None
            restaurant['reviews_total']=None

            """list of types of restaurant"""
            """restaurant['rest_type']=self.__getRestType(hxs.select('//b/text()[contains(.,"Cocina:")]/../..'))"""

            return


        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurant from response: " + str(e),self.utils.ERROR)


    def __getEmail(self,txtEmail):
               
        if len(txtEmail)>0:
            return txtEmail.replace("mailto:","")
           




